<template>
	<el-container class="home-container">
		<!-- 头部区域 -->
		<el-header>
			<div>

				<span>电商后台管理系统</span>
			</div>
			<div>
				<el-button type="info" @click="logout">退出</el-button>
				<el-button type="info" @click="ModifyDialogVisible=true">修改密码</el-button>
			</div>
			
		</el-header>
		<!-- 页面主题区域 -->
		<el-container>
			<!-- 侧边栏 -->
			<el-aside :width="isCollapse ? '64px' : '200px' ">
				<div class="toggle-button" @click="toggleCollapse">
					|||
				</div>
				<!-- 侧边栏菜单区域 -->
				<el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened :collapse="isCollapse"
				 :collapse-transition="false" router :default-active="activePath">
					<!-- 一级菜单 -->
					<el-submenu :index="item.Id+''" v-for="item in menulist" :key="item.Id">
						<!-- 一级菜单的模板区 -->

						<template slot="title">
							<!-- 图标 -->
							<i :class="iconsObj[item.Id]"></i>
							<!-- 文本 -->
							<span>{{item.Name}}</span>
						</template>
						<!-- 二级菜单 -->

						<el-menu-item v-if="item.Id==subItem.ParentId" :index="'/'+subItem.Path" v-for="subItem in Sonlist" :key="subItem.Id"
						 @click="saveNavState('/'+subItem.Path)">
							<template slot="title">
								<!-- 图标 -->
								<i class="el-icon-menu"></i>
								<!-- 文本 -->
								<span>{{subItem.Name}}</span>
							</template></el-menu-item>
					</el-submenu>
				</el-menu>
			</el-aside>
			<!-- 右侧内容字体 -->
			<el-main>
				<!-- 路由占位符 -->
				<router-view></router-view>
			</el-main>
		</el-container>

		<!-- 添加用户的对话框 -->
		<el-dialog title="修改密码" :visible.sync="ModifyDialogVisible" width="50%" @close="ModifyDialogClosed">
			<!-- 内容主题区域 -->
			<el-form :model="ModifyForm" label-width="100px">
				<el-form-item label="请输入原密码" >
					<el-input v-model="ModifyForm.OldPassword"></el-input>
				</el-form-item>
				<el-form-item label="请输入新密码" >
					<el-input v-model="ModifyForm.NewPassword" type="password"></el-input>
				</el-form-item>
				<el-form-item label="再次输入">
					<el-input v-model="ModifyForm.AgainPassword" type="password"></el-input>
				</el-form-item>
				
			</el-form>
			<!-- 底部区域 -->
			<span slot="footer" class="dialog-footer">
				<el-button @click="ModifyDialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="Modify">确定</el-button>
			</span>
		</el-dialog>


	</el-container>
</template>

<script>
	export default {
		data() {
			return {
				menulist: [],
				Sonlist: [],
				iconsObj: {
					'125': 'iconfont icon-user',
					'103': 'iconfont icon-tijikongjian',
					'101': 'iconfont icon-shangpin',
					'102': 'iconfont icon-danju',
					'145': 'iconfont icon-baobiao'
				},
				//是否折叠
				isCollapse: false,
				//被激活的链接地址
				activePath: '',
				//修改密码
				ModifyForm: {
					OldPassword: '',
					NewPassword: '',
					AgainPassword: '',
					//CreateTime:""
				},
				ModifyDialogVisible:false,

			}
		},
		created() {
			this.getMenuList()
			this.activePath = window.sessionStorage.getItem('activePath')
			
		},
		methods: {
			logout() {
				window.sessionStorage.clear()
				this.$router.push('/login')
			},
			//获取所有的菜单
			// getMenuList() {
			               
			//                 this.$http.get("api/Login/GetMenuList").then(function (res) {
			//                     console.log(res)       
			//                     this.menulist = res.data.parent;
			//                     this.Sonlist = res.data.son;
			//                 })
			async getMenuList() {
			const {
			 		data: res
				} = await this.$http.get('api/Login/GetMenuList')
				console.log(res)
				if (res.code !== 0) return this.$message.error(res.meta.msg)
			 	this.menulist = res.parent;
				 this.Sonlist = res.son;

			},
			//点击按钮，切换菜单的折叠和展开
			toggleCollapse() {
				this.isCollapse = !this.isCollapse
			},
			//保存链接的激活状态
			saveNavState(activePath) {
				window.sessionStorage.setItem('activePath', activePath)
				this.activePath = activePath
			},
			async Modify(){
				if(this.ModifyForm.NewPassword==this.ModifyForm.AgainPassword){
					let NewPassword=this.ModifyForm.NewPassword
					let OldPassword=this.ModifyForm.OldPassword
					 this.$http.get('api/Login/GetModifyPwd?OldPassword='+OldPassword+"&NewPassword="+NewPassword).then(res=>{
						 console.log(res);
						 if(res.data.code==0){
							this.$message.success(res.data.msg)
							this.ModifyDialogVisible=false;
						 }
					 })
				}else{
					return this.$message.error('两次密码不一致')
				}
			},
			ModifyDialogClosed() {
				this.ModifyForm.OldPassword= ''
				this.ModifyForm.NewPassword= ''
				this.ModifyForm.AgainPassword= ''
			}

		}
	}
</script>

<style Lang="less" scoped>
	.el-header {
		background-color: #373d41;
		display: flex;
		justify-content: space-between;
		padding-left: 0;
		align-items: center;
		color: #fff;
		font-size: 20px;
	}

	.el-header>div {
		display: flex;
		align-items: center;
	}

	.el-header>div span {
		margin-left: 15px;
	}

	.el-aside {
		background-color: #333744;
	}

	.el-aside el-menu {
		border-right: none;
	}

	.el-main {
		background-color: #eaedf1;
	}

	.home-container {
		height: 100%;
	}

	.iconfont {
		margin-right: 10px;
	}

	.toggle-button {
		background-color: #4A5064;
		font-size: 10px;
		line-height: 24px;
		color: #fff;
		text-align: center;
		letter-spacing: 0.2em;
		cursor: pointer;
	}
</style>
